package com.leaf.spring.y201907.day13.nio;

import java.nio.IntBuffer;

public class LearnBuffer {

	static IntBuffer buf = IntBuffer.allocate(10);

	public static void main(String[] args) {

		System.out.println("1. 写入数据之前" + buf);

		int temp[] = { 5, 7, 9 };
		buf.put(3);
		buf.put(temp);

		System.out.println("2. 写入数据之后" + buf);

		buf.flip(); // 切换到读模式

		System.out.println("3. 准备输出数据时" + buf);

		show();

		System.out.println("重置前" + buf);
		buf.rewind(); // 将position重置为0，一般用于重复读

		System.out.println("重置后" + buf);
		show();

	}

	private static void show() {

		System.out.println("缓冲区中的内容");
		while (buf.hasRemaining()) {
			int x = buf.get();
			System.out.print(x + "、");
		}
		System.out.println();

	}

}
